package bandidong.dao.impl;

import bandidong.dao.CustomerDAO;
import bandidong.model.Customer;
import bandidong.transfermodel.SearchCustomerTransferModel;
import bandidong.utils.Constant;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

public class CustomerDAOImpl implements CustomerDAO {
    
    private EntityManager em;
    
    public CustomerDAOImpl(EntityManager em) {
        this.em = em;
    }
    
    @Override
    public List<Customer> getAllCustomers() {
        try {
            String jpaql = "SELECT c FROM Customer c";
            Query query = em.createQuery(jpaql);
            List<Customer> customers = (List<Customer>) query.getResultList();
            return customers;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public Customer getCustomerById(int customerId) {
        try {
            String jpaql = "SELECT c FROM Customer c WHERE c.customerId = :customerId";
            Query query = em.createQuery(jpaql);
            query.setParameter("customerId", customerId);
            List<Customer> customers = (List<Customer>) query.getResultList();
            if (customers != null && !customers.isEmpty()) {
                return customers.get(0);
            }
            return null;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public Customer addCustomer(Customer customer) {
        try {
            em.persist(customer);
            em.flush();
            return customer;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public String updateCustomer(Customer customer) {
        try {
            Customer c = em.find(Customer.class, customer.getCustomerId());
            c.setCustomerName(customer.getCustomerName());
            c.setCustomerAddress(customer.getCustomerAddress());
            c.setCustomerEmail(customer.getCustomerEmail());
            c.setCustomerAccountNumber(customer.getCustomerAccountNumber());
            c.setCustomerPassword(customer.getCustomerPassword());
            c.setCustomerPhoneNumber(customer.getCustomerPhoneNumber());
            c.setOrdersSet(customer.getOrdersSet());
            em.merge(c);
            return Constant.SUCCESS;
        }
        catch (Exception ex) {
            return Constant.ERROR;
        }
    }

    @Override
    public String deleteCustomer(Customer customer) {
        try {
            Customer c = em.find(Customer.class, customer.getCustomerId());
            em.remove(c);
            return Constant.SUCCESS;
        }
        catch (Exception ex) {
            return Constant.ERROR;
        }
    }

    @Override
    public List<Customer> getCustomersCriteria(SearchCustomerTransferModel model, int page, int pageSize) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Customer> getCustomersCriteria(SearchCustomerTransferModel model) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean checkEmailExists(String customerEmail) {
        try {
            String jpaql = "SELECT c FROM Customer c WHERE c.customerEmail = :customerEmail";
            Query query = em.createQuery(jpaql);
            query.setParameter("customerEmail", customerEmail);
            List<Customer> customers = (List<Customer>) query.getResultList();
            if (customers != null && !customers.isEmpty()) {
                return true;
            }
            return false;
        }
        catch (Exception ex) {
            return true;
        }
    }

    @Override
    public boolean checkAccountNumberExists(String customerAccountNumber) {
        try {
            String jpaql = "SELECT c FROM Customer c WHERE c.customerAccountNumber = :customerAccountNumber";
            Query query = em.createQuery(jpaql);
            query.setParameter("customerAccountNumber", customerAccountNumber);
            List<Customer> customers = (List<Customer>) query.getResultList();
            if (customers != null && !customers.isEmpty()) {
                return true;
            }
            return false;
        }
        catch (Exception ex) {
            return true;
        }
    }

    @Override
    public boolean checkEmailExists(String customerEmail, int customerId) {
        try {
            String jpaql = "SELECT c FROM Customer c WHERE c.customerEmail = :customerEmail AND c.customerId != :customerId";
            Query query = em.createQuery(jpaql);
            query.setParameter("customerEmail", customerEmail);
            query.setParameter("customerId", customerId);
            List<Customer> customers = (List<Customer>) query.getResultList();
            if (customers != null && !customers.isEmpty()) {
                return true;
            }
            return false;
        }
        catch (Exception ex) {
            return false;
        }
    }

    @Override
    public boolean checkAccountNumberExists(String customerAccountNumber, int customerId) {
        try {
            String jpaql = "SELECT c FROM Customer c WHERE c.customerAccountNumber = :customerAccountNumber AND c.customerId != :customerId";
            Query query = em.createQuery(jpaql);
            query.setParameter("customerAccountNumber", customerAccountNumber);
            query.setParameter("customerId", customerId);
            List<Customer> customers = (List<Customer>) query.getResultList();
            if (customers != null && !customers.isEmpty()) {
                return true;
            }
            return false;
        }
        catch (Exception ex) {
            return false;
        }
    }

    @Override
    public Customer checkCustomerExists(String customerEmail, String customerPassword) {
        try {
            String jpaql = "SELECT c FROM Customer c WHERE c.customerEmail = :customerEmail AND c.customerPassword = :customerPassword";
            Query query = em.createQuery(jpaql);
            query.setParameter("customerEmail", customerEmail);
            query.setParameter("customerPassword", customerPassword);
            List<Customer> customers = (List<Customer>) query.getResultList();
            if (customers != null && !customers.isEmpty()) {
                return customers.get(0);
            }
            return null;
        }
        catch (Exception ex) {
            return null;
        }
    }
}
